<template>
  <div class="app-container">
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true" v-show="pd.showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="pd.queryParams.name" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="pd.queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="更新时间" style="width: 308px">
        <el-date-picker v-model="pd.daterangeUpdate_time" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['hrp:partner:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="pd.single" @click="handleUpdate"
          v-hasPermi="['hrp:partner:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="pd.multiple" @click="handleDelete"
          v-hasPermi="['hrp:partner:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['hrp:partner:export']">导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="pd.loading" :data="pd.partnerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :options="hrp_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="社会编码" align="center" prop="idCard" />
      <el-table-column label="诞生日期" align="center" prop="birthday" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="是否启用" align="center" prop="enableFlag">-->
      <!--        <template #default="scope">-->
      <!--          <dict-tag :options="sys_is" :value="scope.row.enableFlag"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="是否客户" align="center" prop="isCustomer">
        <template #default="scope">
          <dict-tag :options="sys_is" :value="scope.row.isCustomer" />
        </template>
      </el-table-column>
      <el-table-column label="是否供应商" align="center" prop="isSupplier">
        <template #default="scope">
          <dict-tag :options="sys_is" :value="scope.row.isSupplier" />
        </template>
      </el-table-column>
      <el-table-column label="是否公司" align="center" prop="isCompany">
        <template #default="scope">
          <dict-tag :options="sys_is" :value="scope.row.isCompany" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['hrp:partner:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['hrp:partner:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
      v-model:limit="pd.queryParams.pageSize" @pagination="getList" />
    <PartnerCurd ref="partnerCurdRef" @ok="partnerCurdOk"></PartnerCurd>
  </div>
</template>

<script setup name="Partner">
import { listPartner, getPartner } from "@/api/hrp/partner";
import PartnerCurd from "@/views/hrp/partner/curd";
import { getCurrentInstance, reactive } from "vue";
const { proxy } = getCurrentInstance();
import { useRoute } from "vue-router";
const route = useRoute();
const { hrp_user_sex, sys_is } = proxy.useDict('hrp_user_sex', 'sys_is');

const pd = reactive({
  partnerList: [],
  loading: true,
  showSearch: true,
  ids: [],
  single: true,
  multiple: true,
  total: 0,
  daterangeUpdate_time: [],
  label: {

  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    mobile: null,
    isSupplier: null,
    isCustomer: null,
    updateTime: null,
    params: []
  },
  rules: {
    enableFlag: [{
      required: true,
      message: "是否不能为空",
      trigger: "change"
    }],
    name: [{
      required: true,
      message: "名称不能为空",
      trigger: "change"
    }],
    mobile: [{
      required: true,
      message: "手机号不能为空",
      trigger: "change"
    }]
  }
});

/** 查询合作伙伴列表 */
function getList() {
  pd.loading = true;
  if (null != pd.daterangeUpdate_time && '' != pd.daterangeUpdate_time) {
    pd.queryParams.params.beginUpdate_time = pd.daterangeUpdate_time[0];
    pd.queryParams.params.endUpdate_time = pd.daterangeUpdate_time[1];
  }
  listPartner(pd.queryParams).then(response => {
    pd.partnerList = response.rows;
    pd.total = response.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  pd.queryParams.params = {};
  pd.daterangeUpdate_time = [];
  proxy.resetForm("queryRef");
  pd.queryParams.isSupplier = route.query.isSupplier;
  pd.queryParams.isCustomer = route.query.isCustomer;``
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  pd.ids = selection.map(item => item.id);
  pd.single = selection.length != 1;
  pd.multiple = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  let partner = {};
  partner.isSupplier = route.query.isSupplier;
  partner.isCustomer = route.query.isCustomer;
  proxy.$refs["partnerCurdRef"].add(partner);
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || pd.ids
  getPartner(_id).then(res => {
    proxy.$refs["partnerCurdRef"].update(res.data);
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || pd.ids;
  proxy.$refs["partnerCurdRef"].del(_ids);
}
/** 对话框完成后刷新界面 */
function partnerCurdOk(operation) {
  getList();
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('hrp/partner/export', {
    ...pd.queryParams
  }, `partner_${new Date().getTime()}.xlsx`)
}
// pd.queryParams.isSupplier = route.query.isSupplier;
// pd.queryParams.isCustomer = route.query.isCustomer;
// console.log(queryParams)
function initPage() {
  pd.queryParams = { ...pd.queryParams, ...route.query };
  if (pd.queryParams.isCustomer == "0") {

  } else if (pd.queryParams.isSupplier == "0") {

  }
}
initPage();
getList();
</script>
